<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 25, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RCoreImporter::cls( "html.ToolbarControl" );
class SysAdminHeader {
	
	/**
	 * Returns a reference to a global JToolBar object, only creating it if it
	 * doesn't already exist.
	 *
	 * @access	public
	 * @return	ToolbarControl	The ToolbarControl object.
	 * 
	 */
	static function &getInstance() {
		static $instances;
		
		if (! isset ( $instances )) {
			$instances = new ToolbarControl ( );
		}
		
		return $instances;
	}
	
	/**
	 * Add Toolbar
	 *
	 * @param string $title
	 * @param Resource $resource
	 * @param image name $icon
	 * @param attribute of link $attribute
	 */
	static function addButton($title, $alias, $resource, $icon = '', $attribute = '') {
		$title = SysLanguage::_ ( $title );
		$bar = & SysAdminHeader::getInstance ();
		$bar->appendButton ( $title, $alias, $resource, $icon, $attribute );
	}
	
	/**
	 * Add button for action
	 *
	 * @param string $title
	 * @param string $act
	 * @param string $attribute
	 */
	static function addActButton($act, $title = '', $attribute = '') {
		if (empty ( $title )) {
			$title = $act;
		}
		if (empty ( $attribute )) {
			$attribute = SysAdminHeader::_getOnclickJavaScript ( $act );
		}
		$title = SysLanguage::_ ( $title );
		$bar = & SysAdminHeader::getInstance ();
		$onclickJs = "systemForm.submitAct('$act');";
		$bar->appendButton ( $title, $act, "javascript:$onclickJs", "blank.gif", $attribute );
	}
	
	/**
	 * Add button for task
	 *
	 * @param string $title
	 * @param string $act
	 * @param string $attribute
	 */
	static function addTaskButton($task, $title = '', $attribute = '') {
		if (empty ( $title )) {
			$title = $task;
		}
		if (empty ( $attribute )) {
			$attribute = SysAdminHeader::_getOnclickJavaScript ( $task );
		}
		$title = SysLanguage::_ ( $title );
		$bar = & SysAdminHeader::getInstance ();
		$onclickJs = "systemForm.submitTask('$task');";
		$bar->appendButton ( $title, $task, "javascript:$onclickJs", "blank.gif", $attribute );
	}
	
	/**
	 * Add button for task
	 *
	 * @param string $title
	 * @param string $act
	 * @param string $attribute
	 */
	static function addOptionButton($option, $title = '', $attribute = '') {
		if (empty ( $title )) {
			$title = $option;
		}
		if (empty ( $attribute )) {
			$attribute = SysAdminHeader::_getOnclickJavaScript ( $option );
		}
		$title = SysLanguage::_ ( $title );
		$bar = & SysAdminHeader::getInstance ();
		$onclickJs = "systemForm.submitOption('$option');";
		$bar->appendButton ( $title, $task, "javascript:$onclickJs", "blank.gif", $attribute );
	}
	
	/**
	 * Get the validate javascript corresponding to an act or task
	 *
	 * @param string $name
	 * @return string
	 */
	static protected function _getOnclickJavaScript($name) {
		$js = "";
		
		switch ($name) {
			case 'edit' :
				$js = 'onclick="return shouldEdit(this);"';
				break;
			case 'delete' :
				$js = 'onclick="return shouldDelete(this);"';
				break;
		}
		
		return $js;
	}
	
	/**
	 * Print Button
	 *
	 */
	static function renderButton() {
		print SysAdminHeader::getButtonHTML ();
	}
	
	/**
	 * Get HTML to show Button
	 *
	 * @return HTML to show button
	 */
	static function getButtonHTML() {
		$bar = & SysAdminHeader::getInstance ();
		return $bar->render ();
	}
	
	/**
	 * Get Button
	 *
	 * @param alias of Button $alias
	 * @return unknown
	 */
	static function getButton($alias) {
		$bar = & SysAdminHeader::getInstance ();
		return $bar->getButton ( $alias );
	}
	
	/**
	 * Edit Button
	 *
	 * @param string $title
	 * @param string $alias
	 * @param Link/ResourceObject $resource
	 * @param image name $icon
	 * @param attribute of Link $attribute
	 * @return unknown
	 */
	static function editButton($title, $alias, $resource, $icon = '', $attribute = '') {
		$bar = & SysAdminHeader::getInstance ();
		return $bar->editButton ( $title, $alias, $resource, $icon, $attribute );
	}
	
	/**
	 * Delete a button
	 *
	 * @param alias of button $alias
	 * @return unknown
	 */
	static function deleteButton($alias) {
		$bar = & SysAdminHeader::getInstance ();
		return $bar->deleteButton ( $alias );
	}
	
	/**
	 * Get All Button
	 *
	 * @return Array of all Button
	 */
	static function getAllButton() {
		$bar = & SysAdminHeader::getInstance ();
		return $bar->getAllButton ();
	}
	
	/**
	 * Set Title of Application
	 *
	 * @param string $title
	 */
	static function setTitle($title) {
		$title = SysLanguage::_ ( $title );
		$bar = & SysAdminHeader::getInstance ();
		return $bar->setTitle ( $title );
	}
	
	/**
	 * Append the title after the existing one. 
	 *
	 * @param string $title
	 * @param string $connector
	 */
	static function appendTitle($title, $connector = ' -> ') {
		$title = SysLanguage::_ ( $title );
		$bar = & SysAdminHeader::getInstance ();
		return $bar->appendTitle($title, $connector);
	}
	
	
	/**
	 * Get title of Application
	 *
	 * @return string $title
	 */
	static function getTitle() {
		$bar = & SysAdminHeader::getInstance ();
		return $bar->getTitle ();
	}
	
	/**
	 * Print title of Application
	 *
	 * @return echo $title
	 */
	static function printTitle() {
		$title = SysAdminHeader::getTitle ();
		SysResponse::appendPageTitle ( $title );
		echo $title;
	}
	
	/**
	 * include mudule
	 *
	 * @param string $module
	 */
	static function includeModule($module) {
		include (SYS_ADMIN_PATH . DS . 'modules' . DS . $module . DS . $module . '.php');
	}
}

?>